﻿<%@ WebHandler Language="C#" Class="Images" %>
using System;
using System.Linq;
using System.Web;
using ImageGallery.DAL;

public class Images : IHttpHandler
{
    public const string PhotoID = "photoId";

    public void ProcessRequest(HttpContext context)
    {
        if (!String.IsNullOrEmpty(context.Request[PhotoID]))
        {
            int photoId = Convert.ToInt32(context.Request[PhotoID]);
            Photo currentPhoto = PhotoDAO.GetPhotoById(photoId);
            byte[] binaryData = currentPhoto.Data;
            if (binaryData != null)
            {
                context.Response.ContentType = "image";
                context.Response.AddHeader("content-disposition", "inline; filename=" + currentPhoto.Title.Replace(' ', '_'));
                context.Response.BinaryWrite(binaryData);
            }
        }
    }

    public bool IsReusable
    {
        get { return true; }
    }
}